package com.xtsoft.platform.base.system.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.druid.support.spring.stat.annotation.Stat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Component
@Configuration
@Slf4j
public class DataSourceConfig {
	/*数据源1参数*/
	@Value("${spring.datasource.primary.url}") private String primaryUrl;
	@Value("${spring.datasource.primary.username}") private String primaryUsername;
	@Value("${spring.datasource.primary.password}") private String primaryPassword;
	@Value("${spring.datasource.primary.driver-class-name}") private String primaryDriverClassName;
	/*数据源2参数*/
	@Value("${spring.datasource.secondary.url}") private String secondaryUrl;
	@Value("${spring.datasource.secondary.username}") private String secondaryUsername;
	@Value("${spring.datasource.secondary.password}") private String secondaryPassword;
	@Value("${spring.datasource.secondary.driver-class-name}") private String secondaryDriverClassName;
	
	/*数据源公共参数*/
	@Value("${spring.datasource.initialSize}") private int initialSize;
	@Value("${spring.datasource.minIdle}") private int minIdle;
	@Value("${spring.datasource.maxActive}") private int maxActive;
	@Value("${spring.datasource.maxWait}") private int maxWait;
	@Value("${spring.datasource.timeBetweenEvictionRunsMillis}") private int timeBetweenEvictionRunsMillis;
	@Value("${spring.datasource.minEvictableIdleTimeMillis}") private int minEvictableIdleTimeMillis;
	@Value("${spring.datasource.validationQuery}") private String validationQuery;
	@Value("${spring.datasource.testWhileIdle}") private boolean testWhileIdle;
	@Value("${spring.datasource.testOnBorrow}") private boolean testOnBorrow;
	@Value("${spring.datasource.testOnReturn}") private boolean testOnReturn;
	@Value("${spring.datasource.poolPreparedStatements}") private boolean poolPreparedStatements;
	@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}") private int maxPoolPreparedStatementPerConnectionSize;
	@Value("${spring.datasource.filters}") private String filters;
	@Value("${spring.datasource.connectionProperties}") private String connectionProperties;
	
	
	/**
	 * 功能: 数据源1 为主数据源
	 * @return DataSource
	 * @date 2018年12月6日 下午3:08:04
	 * @auth kezhuan
	 */
     @Bean(name = "primaryDataSource")//声明其为Bean实例
     @Qualifier("primaryDataSource")
     @Primary  //在同样的DataSource中，首先使用被标注的DataSource
     public DataSource primaryDataSource() {
         DruidDataSource datasource = new DruidDataSource();
         datasource.setUrl(primaryUrl);
         datasource.setUsername(primaryUsername);
         datasource.setPassword(primaryPassword);
         datasource.setDriverClassName(primaryDriverClassName);
         this.setParameter(datasource);
         return (DataSource) datasource;
     }
     
     
     /**
      * 功能: 数据源2
      * @return
      * @return DataSource
      * @date 2018年12月6日 下午3:08:39
      * @auth kezhuan
      */
     @Bean(name = "secondaryDataSource")//声明其为Bean实例
     @Qualifier("secondaryDataSource")
     public DataSource secondaryDataSource()  {
         DruidDataSource datasource = new DruidDataSource();
         datasource.setUrl(secondaryUrl);
         datasource.setUsername(secondaryUsername);
         datasource.setPassword(secondaryPassword);
         datasource.setDriverClassName(secondaryDriverClassName);
         this.setParameter(datasource);
         return (DataSource) datasource;
     }
     
     
     /**
      * 功能: 设置数据源公共参数
      * @param datasource
      * @return void
      * @date 2018年12月6日 下午3:09:02
      * @auth kezhuan
      */
     private void setParameter(DruidDataSource datasource) {
    	 datasource.setInitialSize(initialSize);
         datasource.setMinIdle(minIdle);
         datasource.setMaxActive(maxActive);
         datasource.setMaxWait(maxWait);
         datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
         datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
         datasource.setValidationQuery(validationQuery);
         datasource.setTestWhileIdle(testWhileIdle);
         datasource.setTestOnBorrow(testOnBorrow);
         datasource.setTestOnReturn(testOnReturn);
         datasource.setPoolPreparedStatements(poolPreparedStatements);
         datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
         try {
             datasource.setFilters(filters);
         } catch (SQLException e) {
            log.error("druid configuration initialization filter: " + e);
         }
         datasource.setConnectionProperties(connectionProperties);
	}
}
